Imports System.Data.SqlClient
Imports System
Imports System.Configuration
Public Class DVinculo
    Dim oCnn As New DConexion

    Public Sub cargarVinculo(ByVal vinculo As Entidades.EVinculo, ByVal ocnn As SqlConnection, ByRef tran As SqlTransaction)
        Dim com As New SqlCommand
        Try
            com.Connection = ocnn
            com.Transaction = tran
            com.CommandType = CommandType.StoredProcedure
            com.CommandText = "CargarVinculo"
            'carga los parametros
            com.Parameters.AddWithValue("@idalumno", vinculo.nrolegajo)
            com.Parameters.AddWithValue("@idtutor", vinculo.idtutor)
            com.Parameters.AddWithValue("@parentesco", vinculo.parentesco)
            com.Parameters.AddWithValue("@estado", vinculo.estado)
            com.ExecuteNonQuery()
            com.Dispose()
        Catch ex As Exception
            Throw New ArgumentException(ex.Message)
        End Try
    End Sub
   

    Public Sub modificarVinculo(ByVal vinculo As Entidades.EVinculo)
        Dim com As New SqlCommand
        Try
            oCnn.abrirConexion()
            com.Connection = oCnn.obtenerConexion
            com.CommandType = CommandType.StoredProcedure
            com.CommandText = "ModificarVinculo"
            'carga los parametros
            com.Parameters.AddWithValue("@idalumno", vinculo.nrolegajo)
            com.Parameters.AddWithValue("@idtutor", vinculo.idtutor)
            com.Parameters.AddWithValue("@parentesco", vinculo.parentesco)
            com.Parameters.AddWithValue("@estado", vinculo.estado)
            'executa el comando
            com.ExecuteNonQuery()
            'cierra conexion
            oCnn.cerrarConexion()
        Catch ex As Exception
            Throw New ArgumentException(ex.Message)
        End Try
    End Sub

    Public Sub borrarVinculo(ByVal vinculo As Entidades.EVinculo)
        Dim com As New SqlCommand
        Try
            oCnn.abrirConexion()
            com.Connection = oCnn.obtenerConexion
            com.CommandType = CommandType.StoredProcedure
            com.CommandText = "BorrarVinculo"
            'carga los parametros
            com.Parameters.AddWithValue("@idalumno", vinculo.nrolegajo)
            com.Parameters.AddWithValue("@idtutor", vinculo.idtutor)
            'executa el comando
            com.ExecuteNonQuery()
            'cierra conexion
            oCnn.cerrarConexion()
        Catch ex As Exception
            Throw New ArgumentException(ex.Message)
        End Try
    End Sub

    'SOBRECARGADO
    Public Function buscarVinculos(ByVal vinculo As Entidades.EVinculo) As List(Of Entidades.EVinculo)
        Dim com As New SqlCommand
        Dim coleccion As New List(Of Entidades.EVinculo)
        Dim dr As SqlDataReader
        Try
            oCnn.abrirConexion()
            com.Connection = oCnn.obtenerConexion
            com.CommandType = CommandType.StoredProcedure
            com.CommandText = "BuscarVinculo"
            'carga os parametros
            com.Parameters.AddWithValue("@idalumno", vinculo.nrolegajo)
            com.Parameters.AddWithValue("@idtutor", vinculo.idtutor)
            dr = com.ExecuteReader
            Do While dr.Read
                'creamos un objeto
                Dim objeto As New Entidades.EVinculo
                objeto.idtutor = dr("idalumno")
                objeto.nrolegajo = dr("idtutor")
                objeto.parentesco = dr("parentesco")
                objeto.estado = dr("estado")
                coleccion.Add(objeto)
                objeto = Nothing
            Loop
            'retornamos los valores encontrados
            dr.Close()
            com.Dispose()
            oCnn.cerrarConexion()
            Return coleccion
        Catch ex As Exception
            Throw New ArgumentException(ex.Message)
        Finally
            coleccion = Nothing
        End Try
    End Function

    'SOBRECARGADO
    Public Function buscarVinculos(ByVal condicion As Boolean) As List(Of Entidades.EVinculo)
        Dim coleccion As New List(Of Entidades.EVinculo)
        Dim com As New SqlCommand
        Try
            oCnn.abrirConexion()
            com.Connection = oCnn.obtenerConexion
            com.CommandType = CommandType.StoredProcedure
            com.CommandText = "BuscarVinculosxCond"
            com.Parameters.AddWithValue("@estado", condicion)
            Dim dr As SqlDataReader
            dr = com.ExecuteReader
            Do While dr.Read
                'creamos un objeto
                Dim objeto As New Entidades.EVinculo
                objeto.nrolegajo = dr("idalumno")
                objeto.idtutor = dr("idtutor")
                objeto.parentesco = dr("parentesco")
                objeto.estado = dr("estado")

                coleccion.Add(objeto)
                objeto = Nothing
            Loop
            oCnn.cerrarConexion()
            'retornamos los valores encontrados
            Return coleccion
        Catch ex As Exception
            Throw New ArgumentException(ex.Message)
        Finally
            coleccion = Nothing
        End Try
    End Function

End Class
